//
//  BidInformationViewController.m
//  ZYZB
//
//  Created by ENERGY on 2018/7/10.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "BidInformationViewController.h"
#import "BidInformationTableViewCell.h"
@interface BidInformationViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * footerView;
@property (nonatomic, strong)NSMutableArray * titleArr;//标题内容
@property (nonatomic, strong)NSMutableArray * cellContentArr;//内容
@property (nonatomic, strong)NSMutableDictionary * cellContentDic;//内容
@end

@implementation BidInformationViewController(networking)
//标的系列
- (void)bidInformation{
    [self showprogressHUD];
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{
                           @"userName":[UserModel sharedInstanced].mobile,
                           @"token":[UserModel sharedInstanced].token,
                           @"phone":[UserModel sharedInstanced].mobile,
                           @"investmentid":self.bidID,
                           @"t":timeStr
                           };
    //sign
    NSDictionary * signDic = @{
                               @"userName":[UserModel sharedInstanced].mobile,
                               @"token":[UserModel sharedInstanced].token,
                               @"phone":[UserModel sharedInstanced].mobile,
                               @"investmentid":self.bidID,
                               @"t":timeStr,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/order/index?api=api1003"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
            NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
            NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            if ([compareMD5Str isEqualToString:signResult]) {
                self.cellContentDic = jsonData[@"body"][0];
                [self.tableView reloadData];
                NSLog(@"成功");
            }else{
                NSLog(@"失败");
            }
        }
    }];
}
@end

@implementation BidInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"标的信息";
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    
    [self bidInformation];//网络接口
}
#pragma mark - init
- (NSMutableArray *)titleArr{
    if (!_titleArr) {
        _titleArr = @[@{@"title":@"产品名称"},
                      @{@"title":@"购买金额"},
                      @{@"title":@"平台建议年均出借率"},
                      @{@"title":@"到期收益"},
                      @{@"title":@"投标期限"},
                      @{@"title":@"购买时间"},
                      @{@"title":@"计息时间"},
                      @{@"title":@"到期时间"},].mutableCopy;
    }return _titleArr;
}

- (NSMutableArray *)cellContentArr{
    if (!_cellContentArr) {
        _cellContentArr = @[].mutableCopy;
    }return _cellContentArr;
}
- (NSMutableDictionary *)cellContentDic{
    if (!_cellContentDic) {
        _cellContentDic = @{}.mutableCopy;
    }return _cellContentDic;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVI_HEIGHT - BOTTOM_HEIGHT) style:UITableViewStylePlain];
        _tableView.backgroundColor = getColor(@"f8f8f8");
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = self.footerView;
        [_tableView registerClass:[BidInformationTableViewCell class] forCellReuseIdentifier:NSStringFromClass([BidInformationTableViewCell class])];
    }return _tableView;
}
- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 84/WIDTH_6_SCALE)];
        UIButton * checkInterestBtn = [[UIButton alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, 35/WIDTH_6_SCALE, 65/WIDTH_6_SCALE, 14/WIDTH_6_SCALE)];
        [self button:checkInterestBtn withBGColor:[UIColor clearColor] withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:0 withMasksToBounds:YES withViwe:_footerView];
        [checkInterestBtn setTitle:@"查看返息" forState:UIControlStateNormal];
        checkInterestBtn.titleLabel.font = DEF_FontSize_14;
        [checkInterestBtn addTarget:self action:@selector(interestBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [checkInterestBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        
        UIButton * checkContractBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 18/WIDTH_6_SCALE - checkInterestBtn.width, checkInterestBtn.top, checkInterestBtn.width, checkInterestBtn.height)];
        [self button:checkContractBtn withBGColor:[UIColor clearColor] withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:0 withMasksToBounds:YES withViwe:_footerView];
        [checkContractBtn setTitle:@"查看合同" forState:UIControlStateNormal];
        checkContractBtn.titleLabel.font = DEF_FontSize_14;
        [checkContractBtn addTarget:self action:@selector(contractBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [checkContractBtn setTitleColor:getColor(@"3675bd") forState:UIControlStateNormal];
    }return _footerView;
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BidInformationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BidInformationTableViewCell class]) forIndexPath:indexPath];
    if (indexPath.row == 7) {
        cell.lineView.hidden = YES;
    }
    cell.titleLab.text = [self.titleArr[indexPath.row] objectForKey:@"title"];
    if ([cell.titleLab.text isEqualToString:@"产品名称"]) {
        cell.contentLab.text = self.cellContentDic[@"productName"];
    }
    if ([cell.titleLab.text isEqualToString:@"购买金额"]) {
        cell.contentLab.text = self.cellContentDic[@"productPrice"];
    }
    if ([cell.titleLab.text isEqualToString:@"平台建议年均出借率"]) {
        cell.contentLab.text = self.cellContentDic[@"sulimit"];
    }
    if ([cell.titleLab.text isEqualToString:@"到期收益"]) {
        cell.contentLab.text = self.cellContentDic[@"earnings"];
    }
    if ([cell.titleLab.text isEqualToString:@"投标期限"]) {
        cell.contentLab.text = self.cellContentDic[@"invtime"];
    }
    if ([cell.titleLab.text isEqualToString:@"购买时间"]) {
        cell.contentLab.text = self.cellContentDic[@"buyTime"];
    }
    if ([cell.titleLab.text isEqualToString:@"计息时间"]) {
        cell.contentLab.text = self.cellContentDic[@"proEndTime"];
    }
    if ([cell.titleLab.text isEqualToString:@"到期时间"]) {
        cell.contentLab.text = self.cellContentDic[@"endTime"];
    }
//    if ([cell.titleLab.text isEqualToString:@"产品名称"]) {
//
//    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45/WIDTH_6_SCALE;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 8;
}
#pragma mark - action
- (void)interestBtnClick:(UIButton *)sender{
    NSLog(@"查看返息");
}
- (void)contractBtnClick:(UIButton *)sender{
    NSLog(@"查看合同");
}
@end
